(function(){"use strict";BX.namespace("BX.Landing");var e=BX.Landing.Utils.slice;var t=BX.Landing.Utils.proxy;var i=BX.Landing.Utils.bind;var r=BX.Landing.Utils.addClass;var a=BX.Landing.Utils.removeClass;var o=BX.Landing.Utils.isNumber;var s=BX.Landing.Utils.style;var n=BX.Landing.Utils.data;var l=BX.Landing.Utils.addQueryParams;BX.Landing.TemplatePreview=function(e){this.closeButton=document.querySelector(".landing-template-preview-close");this.createButton=document.querySelector(".landing-template-preview-create");this.createByImportButton=document.querySelector(".landing-template-preview-create-by-import");this.title=document.querySelector(".landing-template-preview-input-title");this.description=document.querySelector(".landing-template-preview-input-description");this.themesPalette=document.querySelector(".landing-template-preview-themes");this.themesSiteColorNode=document.querySelector(".landing-template-preview-site-color");this.themesSiteCustomColorNode=document.querySelector(".landing-demo-preview-custom-color");this.imageContainer=document.querySelector(".preview-desktop-body-image");this.loaderContainer=document.querySelector(".preview-desktop-body-loader-container");this.previewFrame=document.querySelector(".preview-desktop-body-preview-frame");this.baseUrlNode=document.querySelector(".landing-template-preview-base-url");this.siteGroupPalette=document.querySelector(".landing-template-preview-site-group");this.loader=new BX.Loader({});this.messages=e.messages||{};this.loaderText=null;this.progressBar=null;this.IsLoadedFrame=false;this.baseUrl="";this.color=null;this.ajaxUrl="";this.ajaxParams={};this.createStore=BX.type.isBoolean(e.createStore)?e.createStore:false;this.disableStoreRedirect=BX.type.isBoolean(e.disableStoreRedirect)?e.disableStoreRedirect:false;this.disableClickHandler=BX.type.isBoolean(e.disableClickHandler)?e.disableClickHandler:false;this.adminSection=BX.type.isBoolean(e.adminSection)?e.adminSection:null;this.zipInstallPath=e.zipInstallPath?e.zipInstallPath:null;this.siteId=e.siteId||0;this.langId=BX.type.isString(e.langId)?e.langId:"";this.folderId=e.folderId||0;this.urlPreview=e.urlPreview||"";this.onCreateButtonClick=t(this.onCreateButtonClick,this);this.onCancelButtonClick=t(this.onCancelButtonClick,this);this.onColorPickerThemeSelect=t(this.onColorPickerThemeSelect,this);this.onFrameLoad=t(this.onFrameLoad,this);BX.addCustomEvent("BX.Landing.ColorPickerTheme:onSelectColor",this.onColorPickerThemeSelect);this.init();return this};BX.Landing.TemplatePreview.getInstance=function(e){return BX.Landing.TemplatePreview.instance||(BX.Landing.TemplatePreview.instance=new BX.Landing.TemplatePreview(e))};BX.Landing.TemplatePreview.prototype={init:function(){var t=e(this.themesPalette.children);if(this.themesSiteColorNode){t=t.concat(e(this.themesSiteColorNode.children))}if(this.themesSiteCustomColorNode){t=t.concat(e(this.themesSiteCustomColorNode.children))}t.forEach(this.initSelectableItem,this);if(this.siteGroupPalette){var r=e(this.siteGroupPalette.children);r.forEach(this.initSelectableItem,this)}i(this.previewFrame,"load",this.onFrameLoad);i(this.closeButton,"click",this.onCancelButtonClick);if(!this.disableClickHandler){i(this.createButton,"click",this.onCreateButtonClick)}this.setBaseUrl();this.setDefaultColor();this.showPreview();this.buildHeader();if(BX.SidePanel.Instance.isReload===true){this.createButton.click()}},setBaseUrl:function(e){if(e===undefined){this.baseUrl=n(this.baseUrlNode,"data-base-url")}else{this.baseUrl=e}},setColor:function(e){if(e!==undefined){this.color=e}},setDefaultColor:function(){if(this.getActiveColorNode()){this.color=n(this.getActiveColorNode(),"data-value")}},getColor:function(){return this.color},createPreviewUrl:function(){var e={};if(!this.baseUrl){this.setBaseUrl()}if(this.getColor()){e={color:this.getColor()}}return l(this.baseUrl,e)},onFrameLoad:function(){if(this.createStore){new BX.Landing.SaveBtn(document.querySelector(".landing-template-preview-create"))}this.IsLoadedFrame=true},getActiveColorNode:function(){var e=this.themesPalette.querySelector(".active");if(!e&&this.themesSiteColorNode){e=this.themesSiteColorNode.querySelector(".active")}if(!e&&this.themesSiteCustomColorNode){e=this.themesSiteCustomColorNode.querySelector(".active")}return e},getActiveSiteGroupItem:function(){return this.siteGroupPalette.querySelector(".active")},showPreview:function(e){if(e===undefined){e=this.createPreviewUrl()}return this.showLoader().then(this.createFrameIfNeeded()).then(this.loadPreview(e)).then(this.hideLoader())},buildHeader:function(){var e=BX.create("div");new QRCode(e,{text:this.urlPreview,width:156,height:156,colorLight:"transparent"});this.showPopupButton=document.querySelector(".mobile-view");if(this.showPopupButton){var t=BX.PopupWindowManager.create("landing-popup-preview",this.showPopupButton,{content:BX.create("div",{props:{className:"landing-popup-preview-content"},children:[BX.create("div",{props:{className:"landing-popup-preview-title"},text:this.messages.LANDING_TPL_POPUP_TITLE}),BX.create("div",{props:{className:"landing-popup-preview-qr"},children:[e]}),BX.create("div",{props:{className:"landing-popup-preview-text"},text:this.messages.LANDING_TPL_POPUP_TEXT})]}),closeIcon:true,closeByEsc:true,noAllPaddings:true,autoHide:true,animation:"fading-slide",angle:{position:"top",offset:75},minWidth:375,maxWidth:375,contentBackground:"transparent"});this.showPopupButton.addEventListener("click",(function(){t.toggle()}))}},createFrameIfNeeded:function(){return function(){return new Promise(function(e){var t=function(){if(!this.previewFrame){this.previewFrame=BX.create("iframe",{props:{className:"preview-desktop-body-preview-frame"}});this.imageContainer.appendChild(this.previewFrame);i(this.previewFrame,"load",this.onFrameLoad)}if(!this.previewFrame.style.width){void s(this.previewFrame,{width:"100%",height:"calc(100vh - 69px)",border:"none"})}e(this.previewFrame)}.bind(this);if(document.readyState!=="complete"){BX.bind(window,"load",t.bind(this))}else{t()}}.bind(this))}.bind(this)},loadPreview:function(e){return function(){return new Promise(function(t){if(this.previewFrame.src!==e){this.previewFrame.src=e;this.previewFrame.onload=function(){t(this.previewFrame)}.bind(this);return}t(this.previewFrame)}.bind(this))}.bind(this)},showLoader:function(){return new Promise(function(e){void this.loader.show(this.loaderContainer);r(this.imageContainer,"landing-template-preview-overlay");e()}.bind(this))},hideLoader:function(){return function(e){return new Promise(function(t){void this.loader.hide();a(this.imageContainer,"landing-template-preview-overlay");t(e)}.bind(this))}.bind(this)},delay:function(e){e=o(e)?e:0;return function(t){return new Promise((function(i){setTimeout(i.bind(null,t),e)}))}},getValue:function(){var e={};if(this.getActiveColorNode()){if(this.themesSiteColorNode&&this.getActiveColorNode().parentElement===this.themesSiteColorNode){e[this.themesSiteColorNode.dataset.name]=this.getActiveColorNode().dataset.value}if(this.siteGroupPalette){e[this.siteGroupPalette.dataset.name]=this.getActiveSiteGroupItem().dataset.value}e[this.themesPalette.dataset.name]=this.getActiveColorNode().dataset.value;if(this.themesSiteCustomColorNode){e[this.themesPalette.dataset.name]=this.getActiveColorNode().dataset.value}}e[this.title.dataset.name]=this.title.value.replaceAll("&","").replaceAll("?","");e[this.description.dataset.name]=this.description.value;return e},getCreateUrl:function(){return l(this.createButton.getAttribute("href"),this.getValue())},onCancelButtonClick:function(e){e.preventDefault();top.BX.SidePanel.Instance.close()},onCreateButtonClick:function(e){e.preventDefault();if(BX.Dom.hasClass(this.createButton.parentNode,"needed-market-subscription")){top.BX.UI.InfoHelper.show("limit_subscription_market_templates");const e=new Promise(function(e){setInterval((()=>{if(BX.Dom.hasClass(this.createButton,"ui-btn-clock")){e()}}),500)}.bind(this));e.then((()=>{BX.Dom.removeClass(this.createButton,"ui-btn-clock");BX.Dom.attr(this.createButton,"style","")}));return}const t=new BX.Landing.Metrika(true);t.sendLabel(null,"createTemplate",e.target.href);if(this.isStore()&&this.IsLoadedFrame){this.loaderText=BX.create("div",{props:{className:"landing-template-preview-loader-text"},text:this.messages.LANDING_LOADER_WAIT});this.progressBar=new BX.UI.ProgressBar({column:true});this.progressBar.getContainer().classList.add("ui-progressbar-landing-preview");this.loaderContainer.appendChild(this.loaderText);this.loaderContainer.appendChild(this.progressBar.getContainer())}if(this.isStore()){if(this.IsLoadedFrame){this.showLoader().then((()=>{this.initCatalogParams();this.createCatalog()}))}}else if(this.zipInstallPath){this.finalRedirectAjax(this.getCreateUrl())}else{this.showLoader().then(this.delay(200)).then(function(){this.finalRedirectAjax(this.getCreateUrl())}.bind(this))}},initCatalogParams:function(){if(this.createButton.hasAttribute("data-href")){this.ajaxUrl=this.createButton.getAttribute("data-href")}this.ajaxParams=this.getValue();this.ajaxParams["start"]="Y";this.ajaxParams["showcaseId"]="clothes"},createCatalog:function(){if(this.ajaxUrl===""){this.hideLoader();return}BX.ajax({method:"POST",dataType:"json",url:this.ajaxUrl,data:BX.ajax.prepareData(this.ajaxParams),onsuccess:BX.proxy(this.createCatalogResult,this)})},createCatalogResult:function(e){if(e.status==="continue"){this.ajaxParams["start"]="N";this.progressBar.update(e.progress);this.progressBar.setTextAfter(e.message);this.createCatalog()}else{this.finalRedirectAjax(e.url)}},finalRedirectAjax:function(e){if(this.zipInstallPath){let t=[];const i=this.getValue();for(let e in i){t["additional["+e+"]"]=i[e]}t["additional[siteId]"]=this.siteId;t["additional[folderId]"]=this.folderId;t["from"]=this.createParamsStrFromUrl(e);if(this.adminSection&&this.langId!==""){t["lang"]=this.langId}if(typeof top.BX.SidePanel!=="undefined"){const e=document.querySelector(".landing-popup-import");const i=document.querySelector(".landing-popup-import-loader");const r=document.querySelector(".preview-left");if(r&&i){this.loader.show(i);BX.Dom.addClass(r,"landing-import-start")}t["inSlider"]="N";if(this.siteId!==0){t["createType"]="PAGE"}let a;BX.ajax({method:"POST",dataType:"html",url:l(this.zipInstallPath,t),onsuccess:t=>{const r=new Promise(((i,r)=>{const o=BX.Dom.create("div",{html:t});BX.Dom.style(o,"display","none");e.append(o);let s;let n=0;a=setInterval((()=>{if(n>100){r(new Error("Time is up"))}s=o.querySelector(".rest-configuration-wrapper");if(s!==null){i(s)}n++}),300)}));r.then((t=>{clearInterval(a);if(BX.Dom.hasClass(t,"rest-configuration-wrapper")){const r=t.querySelector(".rest-configuration-title");const a=t.querySelector(".rest-configuration-start-icon-main-container");if(r&&a){BX.Dom.remove(r);BX.Dom.insertBefore(r,a.nextSibling)}this.loader.hide();BX.Dom.append(t,e);BX.Dom.style(i,"display","none")}}),(e=>{clearInterval(a);this.addRepeatCreateButton()}))}})}}else if(this.disableStoreRedirect){BX.ajax({method:"POST",dataType:"html",url:e,onsuccess:function(){if(typeof top.BX.SidePanel!=="undefined"){setTimeout((function(){top.BX.onCustomEvent("Landing:onDemoCreateStart");top.BX.SidePanel.Instance.close()}),100)}}})}else{window.location=e}},addRepeatCreateButton:function(){const e=document.querySelector(".landing-popup-import-repeat");if(e){BX.Dom.removeClass(e,"hide")}const t=document.querySelector(".landing-popup-import-repeat-button");if(t){i(t,"click",this.onRepeatButtonClick)}},onRepeatButtonClick:function(){const e=document.querySelector(".landing-popup-import-repeat");if(e){BX.Dom.addClass(e,"hide")}const t=document.querySelector(".landing-template-preview-create");if(t){t.click()}},initSelectableItem:function(e){i(e,"click",t(this.onSelectableItemClick,this))},onSelectableItemClick:function(e){e.preventDefault();if(e.currentTarget.parentElement===this.themesPalette||this.themesSiteColorNode&&e.currentTarget.parentElement===this.themesSiteColorNode){if(this.getActiveColorNode()){this.getActiveColorNode().classList.remove("active")}r(e.currentTarget,"active");this.setColor(n(e.currentTarget,"data-value"));this.showPreview()}if(e.currentTarget.parentElement===this.siteGroupPalette){a(this.getActiveSiteGroupItem(),"active");r(e.currentTarget,"active");this.setBaseUrl(n(e.currentTarget,"data-base-url"));this.showPreview()}},isStore:function(){return this.createStore},onColorPickerThemeSelect:function(e){[this.themesPalette,this.themesSiteCustomColorNode,this.themesSiteColorNode].forEach((function(e){if(e){BX.removeClass(e.querySelector(".active"),"active")}}));e.data.node.classList.add("active");var t=new BX.Loader({});var i=document.querySelector(".preview-desktop-body-loader-container");t.show(i);var o=document.querySelector(".preview-desktop-body-image");r(o,"landing-template-preview-overlay");var s=document.querySelector(".preview-desktop-body-preview-frame");if(s){var n=new URL(s.getAttribute("src"));var l=new URLSearchParams(n.search);l.set("color",e.data.color.substr(1));n.search=l.toString();s.setAttribute("src",n.toString());setTimeout(d,1600)}function d(){t.hide();a(o,"landing-template-preview-overlay")}},createParamsStrFromUrl(e){var t=e.match(/&app_code=[^&]+/i);var i="";if(t!==null){i=t[0].substr(10)}var r=e.match(/&title=[^&]+/iu);var a="";if(r!==null){a=r[0].substr(7)}var o=e.match(/&preview_id=[^&]+/i);var s="";if(o!==null){s=o[0].substr(12)}return"|"+i+"|"+a+"|"+s}}})();
//# sourceMappingURL=script.map.js